from ninja import Schema
from pydantic import BaseModel, ConfigDict, TypeAdapter

# from datetime import datetime
# from django.utils import timezone
from typing import List, Literal
from typing_extensions import TypedDict
# from pydantic import Field
from ninja import ModelSchema
from .models import ChatUser, ChatGroup, ChatMessage, UserToGroup


class UserSchemaResp(ModelSchema):
    class Meta:
        model = ChatUser
        fields = "__all__"


class CreateUserSchema(Schema):
    username: str
    phone: str
    first_name: str = ""
    last_name: str = ""
    email: str = ""
    password: str
    is_superuser: bool = False


class LoginSchema(Schema):
    username: str
    password: str


class CreateGroupSchema(Schema):
    name: str
    leader: int
    number_of_people: int


class GroupSchemaResp(ModelSchema):
    class Meta:
        model = ChatGroup
        fields = [
            "id",
            "name",
            "leader",
            "number_of_people",
            "verbose",
            "created_at",
            "updated_at",
            "deleted",
        ]


class CreateMessageSchema(Schema):
    user_id: int
    group_id: int
    sender: str
    content: str
    withdraw: bool = False


class MessageSchemaResp(ModelSchema):
    class Meta:
        model = ChatMessage
        fields = ["id", "user_id", "group_id", "sender", "content", "withdraw"]


class CreateUserToGroupSchema(Schema):
    user_id: int
    group_id: int


class UserToGroupSchemaResp(ModelSchema):
    class Meta:
        model = UserToGroup
        fields = "__all__"


class UserCreateMessageSchema(ModelSchema):
    class Meta:
        model = ChatMessage
        fields = [
            "user_id",
            "group_id",
            "sender",
            "content",
        ]


class MessagesSchemaResp(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    withdraw: bool 
    content: str 
    sender: str 
    user_id: int 
    group_id: int 
    id: int = 0

class MessagesListSchemaResp(BaseModel):
    messages: List[MessagesSchemaResp] = None


class ChatOnopenSchemaResp(BaseModel):
    type: Literal["onopen", "onclose", "onmessage", "error"]
    data_list: List[MessagesListSchemaResp]

class ChatConnectGroupMix(TypedDict):
    name: str
    leader: str

class ChatConnectSchemaResp(TypedDict):
    message: str
    group: ChatConnectGroupMix

class ChatOncloseSchemaResp(BaseModel):
    pass

class ChatSendMessageSchemaResp(BaseModel):
    type: Literal["onopen", "onclose", "onmessage", "error"]
    message: str

class ChatOnmessageSchemaResp(BaseModel):
    pass


class ChatErrorSchemaResp(TypedDict):
    error: str



# type_chat_error = TypeAdapter(ChatErrorSchemaResp)
# type_chat_connect = TypeAdapter(ChatConnectSchemaResp)
